﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ResourceTranslator
{
    /// <summary>
    /// Interaction logic for FieldViewControl.xaml
    /// </summary>
    public partial class FieldViewControl : UserControl
    {
        public FieldViewControl()
        {
            InitializeComponent();
        }

        public bool ReadOnly { get; set; }

        public string Label { get; set; }

        Type dataType;
        public Type DataType
        {
            get { return dataType; }
            set
            {
                dataType = value;
                OnInitialized(EventArgs.Empty);
            }
        }

        object data;
        public object Data
        {
            get { return data; }
            set
            {
                data = value;
                OnInitialized(EventArgs.Empty);
            }
        }

        UIElement Editor;

        protected override void OnInitialized(EventArgs e)
        {
            if (DataType == null)
                return;

            if (DataType.BaseType == typeof(Enum))
            {
                ComboBox cb = new ComboBox();
                cb.ItemsSource = Enum.GetValues(DataType);
                cb.SelectedItem = Data;
                Editor = cb;
            }
            else if (DataType == typeof(bool))
            {
                ComboBox cb = new ComboBox();
                cb.ItemsSource = new bool[] { true, false };
                cb.SelectedItem = Data;
                Editor = cb;
            }
            else
            {
                TextBox tb = new TextBox();
                tb.Text = Data != null ? Data.ToString() : "";
                Editor = tb;
            }

            labeledControl.Control = Editor;

            base.OnInitialized(e);
        }
    }
}
